use crate::config::create_settings_service;
use crate::utils::{download_image, ImageMessageItem};
use std::path::Path;

pub fn download_images_task_command(item: ImageMessageItem) {
    let url = item.url;
    let path = item.path;
    let settings_service = create_settings_service();
    let proxy_str = settings_service.get_setting("proxy").unwrap();
    let image_save_path = settings_service.get_setting("image_save_path").unwrap();

    if proxy_str.is_none() || image_save_path.is_none() {
        println!("proxy or image_save_path is None");
    } else {
        let proxy = proxy_str.unwrap();
        let image_save_path = image_save_path.unwrap();
        let save_path = format!("{}/{}", image_save_path, path);
        let save_dir = Path::new(&save_path).parent().unwrap();
        if !save_dir.exists() {
            std::fs::create_dir_all(save_dir).unwrap();
        }
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(async move {
            download_image(&url, &save_path, &proxy).await;
        });
    }
}
